package com.rainy.sso.server.web;

import com.alibaba.fastjson.JSON;
import com.rainy.sso.client.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;
import com.rainy.sso.client.entity.User;
import javax.annotation.Resource;

/**
 * @author wb-lq289741 on 2017/12/10
 */
@Api(value = "测试api",description = "用户接口测试")
@RestController
@RequestMapping("users")
public class AuthController {

    @Resource
    private UserService userService;

    @ApiOperation(value = "获取用户信息",notes = "根据id获取用户信息")
    @ApiImplicitParam(name = "id",value = "用户id",dataType = "Long",required = true,paramType = "path")
    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public User get(@PathVariable(name = "id") Long id){
        return userService.getUserById(id);
    }

    @ApiOperation(value = "新增用户",notes = "新增用户")
    @ApiImplicitParams({@ApiImplicitParam(name = "user",value = "用户信息",dataType = "User",required = true,paramType = "body")})
    @RequestMapping(value = "",method = RequestMethod.PUT)
    public String add(@RequestBody User user){
        return  "test"+user.getNickname();
    }

    @ApiOperation(value = "更新用户信息",notes = "更新用户信息")
    @ApiImplicitParams({@ApiImplicitParam(name = "user",value = "用户信息",dataType = "User",required = true,paramType = "body")})
    @RequestMapping(value = "",method = RequestMethod.POST)
    public String update(@RequestBody User user){
        return "test"+user.getNickname();
    }

    @ApiOperation(value = "删除用户",notes = "根据id删除用户")
    @ApiImplicitParam(name = "id",value = "用户id",dataType = "Long",required = true,paramType = "path")
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable(name = "id") Long id){
        return "test"+id;
    }

    @ApiOperation(value = "testShiro",notes = "testShiro")
    @ApiImplicitParam(name = "id",value = "用户id",dataType = "Long",required = true,paramType = "path")
    @RequestMapping(value = "testShiro/{id}",method = RequestMethod.GET)
    public Object testShiro(@PathVariable(name = "id") Long id){
        UsernamePasswordToken usernamePasswordToken=new UsernamePasswordToken("liuqin","12334");
        Subject subject = SecurityUtils.getSubject();
        return JSON.toJSONString(subject);
    }
}
